package com.jk.vehicle.mini.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jk.vehicle.mini.entity.AdPreviewRecord;
import com.jk.vehicle.mini.entity.User;
import com.jk.vehicle.mini.feign.FeignClientApp;
import com.jk.vehicle.mini.service.AdPreviewRecordService;
import com.jk.vehicle.mini.service.CorpService;
import com.jk.vehicle.mini.service.UserService;
import com.jk.vehicle.mini.vo.AdSettingSelectListAppVo;
import com.jk.vehicle.core.controller.BaseController;
import com.jk.vehicle.core.entity.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* app:广告浏览记录控制层
*
* @author jiajiang
* @since 2021-05-07
*/
@Api(value="广告浏览记录基础接口", tags = "广告浏览记录基础接口")
@RestController
@RequestMapping("/adpreviewrecord")
public class AdPreviewRecordController extends BaseController {

    @Resource
    private AdPreviewRecordService adPreviewRecordService;

    @Autowired
    private FeignClientApp feignClientApp;

    @Resource
    private CorpService corpService;

    @Resource
    private UserService userService;

    /**
     * 新增adpreviewrecord√
     * @param adPreviewRecord
     * @return
     */
    @ApiOperation(value = "新增广告浏览|点击记录", notes = "新增广告浏览|点击记录 jiajiang 2021-06-09")
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody AdPreviewRecord adPreviewRecord) {
        if (adPreviewRecord.getCorpId().longValue() == 0 || adPreviewRecord.getCorpId() == null) {
            return failResult("厂家id不能为空！");
        }
        if (adPreviewRecord.getCorpId() == -1) {
            return failResult("厂家id不能等于-1！");
        }
        User user = userService.findById(adPreviewRecord.getUserId(), null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        String dsName = corpService.selectDsNameById(adPreviewRecord.getCorpId());
        if (adPreviewRecord.getAdId() == null || adPreviewRecord.getAdId().longValue() == 0) {
            return failResult("关联广告id不能为空！");
        }
        adPreviewRecordService.save(adPreviewRecord, dsName);
        return successResult();
    }

    /**
     * 查询广告设置列表√
     * @return
     */
    @ApiOperation(response = AdSettingSelectListAppVo.class, value = "查询广告设置列表", notes = "查询广告设置列表 jiajiang 2021-06-09")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", defaultValue = "", value = "用户id", dataType = "Long"),
            @ApiImplicitParam(name = "lng", defaultValue = "", value = "经度", dataType = "String"),
            @ApiImplicitParam(name = "lat", defaultValue = "", value = "纬度", dataType = "String"),
            @ApiImplicitParam(name = "corpId", defaultValue = "", value = "厂家ID", dataType = "Long")
    })
    @GetMapping(value = "/selectAdSettingList")
    public ResponseEntity<List<AdSettingSelectListAppVo>> selectAdSettingList(@RequestParam(name = "userId", defaultValue = "") Long userId,
                                                                              @RequestParam(name = "lng", defaultValue = "") String lng,
                                                                              @RequestParam(value = "lat", defaultValue = "") String lat,
                                                                              @RequestParam(value = "corpId", defaultValue = "") Long corpId) {
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        if (corpId == -1) {
            return failResult("厂家id不能等于-1！");
        }
        User user = userService.findById(userId, null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        String dsName = corpService.selectDsNameById(corpId);
        List<AdSettingSelectListAppVo> adSettingSelectListAppVoList = feignClientApp.selectAdSettingList(lng, lat, corpId);
        if (CollectionUtils.isNotEmpty(adSettingSelectListAppVoList)) {
            if (userId != null && userId.longValue() != 0) {
                adSettingSelectListAppVoList.forEach(adSettingSelectListAppVo -> {
                    AdPreviewRecord adPreviewRecord = new AdPreviewRecord();
                    adPreviewRecord.setAdId(adSettingSelectListAppVo.getId());
                    adPreviewRecord.setUserId(userId);
                    adPreviewRecord.setOperationType(0);
                    adPreviewRecordService.save(adPreviewRecord, dsName);
                });
            }
        }
        return successResult(adSettingSelectListAppVoList);
    }

 }
